// internal/domain/storage.go
package domain

import (
	"context"
	"time"
)

// StoragePoolService 存储池服务接口
type StoragePoolService interface {
	// 存储池管理
	ListPools(ctx context.Context, connID string) ([]*StoragePoolInfo, error)
	GetPool(ctx context.Context, connID, poolName string) (*StoragePoolInfo, error)
	CreatePool(ctx context.Context, connID string, config *StoragePoolConfig) (*StoragePoolInfo, error)
	DeletePool(ctx context.Context, connID, poolName string) error

	// 存储池操作
	StartPool(ctx context.Context, connID, poolName string) error
	StopPool(ctx context.Context, connID, poolName string) error
	RefreshPool(ctx context.Context, connID, poolName string) error
	SetAutostart(ctx context.Context, connID, poolName string, autostart bool) error

	// 存储卷管理
	ListVolumes(ctx context.Context, connID, poolName string) ([]*StorageVolumeInfo, error)
	GetVolume(ctx context.Context, connID, poolName, volumeName string) (*StorageVolumeInfo, error)
	CreateVolume(ctx context.Context, connID, poolName string, config *StorageVolumeConfig) (*StorageVolumeInfo, error)
	CloneVolume(ctx context.Context, connID, poolName, srcVolume, destVolume string) error
	DeleteVolume(ctx context.Context, connID, poolName, volumeName string) error
	ResizeVolume(ctx context.Context, connID, poolName, volumeName string, newSize uint64) error
}

// StoragePoolInfo 存储池信息
type StoragePoolInfo struct {
	Name       string           `json:"name"`
	UUID       string           `json:"uuid"`
	State      StoragePoolState `json:"state"`
	Type       string           `json:"type"`
	Capacity   uint64           `json:"capacity"`   // bytes
	Allocation uint64           `json:"allocation"` // bytes
	Available  uint64           `json:"available"`  // bytes
	Autostart  bool             `json:"autostart"`
	Persistent bool             `json:"persistent"`
	Path       string           `json:"path"`
	Volumes    int              `json:"volumes"`
	CreatedAt  time.Time        `json:"created_at"`
}

type StoragePoolState string

const (
	PoolStateInactive     StoragePoolState = "inactive"
	PoolStateBuilding     StoragePoolState = "building"
	PoolStateRunning      StoragePoolState = "running"
	PoolStateDegraded     StoragePoolState = "degraded"
	PoolStateInaccessible StoragePoolState = "inaccessible"
)

// StoragePoolConfig 存储池配置
type StoragePoolConfig struct {
	Name      string            `json:"name" binding:"required"`
	Type      string            `json:"type" binding:"required"` // dir, fs, netfs, logical, disk, iscsi, scsi, rbd, sheepdog, gluster, zfs
	Target    *PoolTargetConfig `json:"target"`
	Source    *PoolSourceConfig `json:"source,omitempty"`
	Autostart bool              `json:"autostart"`
}

type PoolTargetConfig struct {
	Path        string                 `json:"path"`
	Permissions *PoolPermissionsConfig `json:"permissions,omitempty"`
}

type PoolPermissionsConfig struct {
	Mode  string `json:"mode"`  // e.g., "0755"
	Owner int    `json:"owner"` // UID
	Group int    `json:"group"` // GID
}

type PoolSourceConfig struct {
	Host   string                  `json:"host,omitempty"`
	Dir    string                  `json:"dir,omitempty"`
	Device []PoolSourceDevice      `json:"devices,omitempty"`
	Name   string                  `json:"name,omitempty"`
	Format *PoolSourceFormatConfig `json:"format,omitempty"`
}

type PoolSourceDevice struct {
	Path string `json:"path"`
}

type PoolSourceFormatConfig struct {
	Type string `json:"type"`
}

// StorageVolumeInfo 存储卷信息
type StorageVolumeInfo struct {
	Name       string            `json:"name"`
	Key        string            `json:"key"`
	Path       string            `json:"path"`
	Type       StorageVolumeType `json:"type"`
	Format     string            `json:"format"`
	Capacity   uint64            `json:"capacity"`   // bytes
	Allocation uint64            `json:"allocation"` // bytes
	Target     VolumeTargetInfo  `json:"target"`
	CreatedAt  time.Time         `json:"created_at"`
}

type StorageVolumeType string

const (
	VolumeTypeFile    StorageVolumeType = "file"
	VolumeTypeBlock   StorageVolumeType = "block"
	VolumeTypeDir     StorageVolumeType = "dir"
	VolumeTypeNetwork StorageVolumeType = "network"
)

type VolumeTargetInfo struct {
	Path        string `json:"path"`
	Format      string `json:"format"`
	Permissions struct {
		Mode  string `json:"mode"`
		Owner int    `json:"owner"`
		Group int    `json:"group"`
	} `json:"permissions"`
}

// StorageVolumeConfig 存储卷配置
type StorageVolumeConfig struct {
	Name       string `json:"name" binding:"required"`
	Capacity   uint64 `json:"capacity" binding:"required"` // bytes
	Allocation uint64 `json:"allocation"`                  // bytes (预分配)
	Format     string `json:"format"`                      // qcow2, raw, etc.
}
